import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout, getParentLayout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/index',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/dashboard',
    component: Layout,
    name: 'Dashboard',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/Dashboard/Index.vue'),
        name: 'Dashboard',
        meta: {
          title: '控制面板',
          noCache: true,
          affix: true,
          icon: 'ant-design:dashboard-filled',
          alwaysShow: true
        }
      }
    ]
  },{
    path: '/balance',
    component: Layout,
    redirect: '/balance/region/list',
    name: 'balance',
    meta: {
      title: '负载管理',
      icon: 'ic:outline-webhook',
      alwaysShow: true
    },
    children: [
      {
        path: 'region/list',
        component: () => import('@/views/balance/region/list.vue'),
        name: 'balance-region',
        meta: {
          title: '域管理'
        }
      },
      {
        path: 'group/list',
        component: () => import('@/views/balance/group/list.vue'),
        name: 'balance-group',
        meta: {
          title: '元端组管理'
        }
      },
      {
        path: 'meta/list',
        component: () => import('@/views/balance/meta/list.vue'),
        name: 'balance-meta',
        meta: {
          title: '元端管理'
        }
      }
    ]
  },{
    path: '/manage',
    component: Layout,
    redirect: '/manage/work/list',
    name: 'manage',
    meta: {
      title: '设备管理',
      icon: 'ic:outline-webhook',
      alwaysShow: true
    },
    children: [
      {
        path: 'work/list',
        component: () => import('@/views/manage/work/list.vue'),
        name: 'manage-work',
        meta: {
          title: '沙箱管理'
        }
      },
      {
        path: 'data/list',
        component: () => import('@/views/manage/data/list.vue'),
        name: 'manage-data',
        meta: {
          title: '秘文端管理'
        }
      }
    ]
  },{
    path: '/auth',
    component: Layout,
    redirect: '/auth/role/list',
    name: 'auth',
    meta: {
      title: '权限管理',
      icon: 'ic:outline-webhook',
      alwaysShow: true
    },
    children: [
      {
        path: '/auth/role/list',
        component: () => import('@/views/auth/role/list.vue'),
        name: 'auth-role',
        meta: {
          title: '角色管理'
        }
      },
      {
        path: '/auth/admin/list',
        component: () => import('@/views/auth/admin/list.vue'),
        name: 'auth-admin',
        meta: {
          title: '管理员管理'
        }
      },
      {
        path: '/auth/ip/list',
        component: () => import('@/views/auth/ip/list.vue'),
        name: 'auth-ip',
        meta: {
          title: '状态管理IP分组'
        }
      },
      {
        path: '/auth/log/list',
        component: () => import('@/views/auth/log/list.vue'),
        name: 'auth-log',
        meta: {
          title: '管理端操作日志'
        }
      },
      {
        path: '/group/org/list',
        component: () => import('@/views/group/org/list.vue'),
        name: 'auth-org',
        meta: {
          title: '组织分组管理'
        }
      }
    ]
  },{
    path: '/config-config',
    component: Layout,
    redirect: '/config/config',
    name: 'config',
    meta: {
      title: '系统设置',
      icon: 'ic:outline-webhook',
      alwaysShow: true
    },
    children: [
      {
        path: '/config/config',
        component: () => import('@/views/config/config.vue'),
        name: 'config-config',
        meta: {
          title: '管理设置'
        }
      }
      /* , {
        path: '/leakage_tmp/list',
        component: () => import('@/views/leakage_tmp/list.vue'),
        name: 'leakage_tmp-list',
        meta: {
          title: '防泄漏模板'
        }
      } */
    ]
  },
  {
    path: '/config-reset',
    component: Layout,
    redirect: '/config/reset',
    name: 'config/reset',
    meta: {},
    children: [
      {
        path: '/config/reset',
        component: () => import('@/views/config/reset.vue'),
        name: 'config-reset',
        meta: {
          title: "重置密码",
          icon: 'cib:telegram-plane'
        }
      }
    ]
  },{
    path: '/share-fx_list',
    component: Layout,
    redirect: '/share/fx_list',
    name: 'share',
    meta: {
      title: '数据管理',
      icon: 'ic:outline-webhook',
      alwaysShow: true
    },
    children: [
      {
        path: '/share/fx_list',
        component: () => import('@/views/share/fx_list.vue'),
        name: 'share-fx_list',
        meta: {
          title: '分享管理'
        }
      }
    ]
  },{
    path: '/org-list',
    component: Layout,
    redirect: '/org/list',
    name: 'platform',
    meta: {
      title: '平台管理',
      icon: 'ic:outline-webhook',
      alwaysShow: true
    },
    children: [
      {
        path: '/org/list',
        component: () => import('@/views/org/list.vue'),
        name: 'platform-org',
        meta: {
          title: '组织机构管理'
        }
      },
      {
        path: '/config/org',
        component: () => import('@/views/config/org.vue'),
        name: 'config-org',
        meta: {
          title: '组织模板'
        }
      }
    ]
  },{
    path: '/leakage-list',
    component: Layout,
    redirect: '/leakage/list',
    name: 'clgl',
    meta: {
      title: '策略管理',
      icon: 'ic:outline-webhook',
      alwaysShow: true
    },
    children: [
      {
        path: '/leakage/list',
        component: () => import('@/views/leakage/list.vue'),
        name: 'leakage-list',
        meta: {
          title: '策略管理'
        }
      },
      {
        path: '/md5/list',
        component: () => import('@/views/md5/list.vue'),
        name: 'md5-list',
        meta: {
          title: 'MD5管理'
        }
      },
      {
        path: '/name/list',
        component: () => import('@/views/name/list.vue'),
        name: 'name-list',
        meta: {
          title: 'NAME管理'
        }
      }
    ]
  },{
    path: '/snapshot',
    component: Layout,
    redirect: '/snapshot/pan/list',
    name: 'snapshot',
    meta: {
      title: '快照与恢复',
      icon: 'ic:outline-webhook',
      alwaysShow: true
    },
    children: [
      {
        path: '/snapshot/pan/list',
        component: () => import('@/views/snapshot/pan/list.vue'),
        name: 'pan-list',
        meta: {
          title: '虚拟盘快照'
        }
      },
      {
        path: '/snapshot/once/file',
        component: () => import('@/views/snapshot/once/file.vue'),
        name: 'once-file',
        meta: {
          title: '单文件版本'
        }
      }
    ]
  },
  {
    path: '/error',
    component: Layout,
    redirect: '/error/404',
    name: 'Error',
    meta: {
      hidden: true,
      title: t('router.errorPage'),
      icon: 'ci:error',
      alwaysShow: false
    },
    children: [
      {
        path: '404-demo',
        component: () => import('@/views/Error/404.vue'),
        name: '404Demo',
        meta: {
          hidden: true,
          title: '404'
        }
      },
      {
        path: '403-demo',
        component: () => import('@/views/Error/403.vue'),
        name: '403Demo',
        meta: {
          hidden: true,
          title: '403'
        }
      },
      {
        path: '500-demo',
        component: () => import('@/views/Error/500.vue'),
        name: '500Demo',
        meta: {
          hidden: true,
          title: '500'
        }
      }
    ]
  }
  // {
  //   path: '/authorization',
  //   component: Layout,
  //   redirect: '/authorization/user',
  //   name: 'Authorization',
  //   meta: {
  //     title: t('router.authorization'),
  //     icon: 'eos-icons:role-binding',
  //     alwaysShow: true
  //   },
  //   children: [
  //     {
  //       path: 'user',
  //       component: () => import('@/views/Authorization/User.vue'),
  //       name: 'User',
  //       meta: {
  //         title: t('router.user')
  //       }
  //     },
  //     {
  //       path: 'role',
  //       component: () => import('@/views/Authorization/Role.vue'),
  //       name: 'Role',
  //       meta: {
  //         title: t('router.role')
  //       }
  //     }
  //   ]
  // }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
